<?php

class Field extends AppModel {

    var $name = 'Field';
    var $belongsTo = array(
        'Category' => array(
            'className' => 'Category',
            'foreignKey' => 'category_id'
        ),
        'Label' => array(
            'className' => 'Label',
            'foreignKey' => 'label_id'
        ),
    );
    var $hasOne = array(
        'ProductAdditionalFieldValue' => array(
            'class' => 'ProductAdditionalFieldValue',
            'foreignKey' => 'field_id'
            ));

    function total_additional_fields($id = null) {
        $this->recursive = -1;
        return $this->find('count', array('conditions' => array('Field.category_id' => $id)));
    }

    function fetch_labels_fields_details($id=null, $id2=null, $id3=null) {
        $this->recursive = -1;
        $options['order'] = array('Field.id' => 'asc');
        $options['conditions'] = array('Field.label_id' => $id, 'Field.category_id' => $id2);

        if (!empty($id3)) {
            $options['joins'] = array(
                array('table' => 'authentic_product_additional_field_values',
                    'alias' => 'ProductAdditionalFieldValue',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'ProductAdditionalFieldValue.product_id =' . $id3,
                        'ProductAdditionalFieldValue.field_id = Field.id',
                    )
                )
            );
            $options['fields'] = array("Field.id", "Field.category_id", "Field.field_name", "Field.field_type", "Field.field_value", "Field.required", 'ProductAdditionalFieldValue.values', 'ProductAdditionalFieldValue.id');
        }
        return $this->find('all', $options);
    }

    function fetch_labels_fields($id=null, $id2=null, $id3=null) {
        $this->recursive = -1;
        $options['order'] = array('Field.id' => 'asc');
        $options['conditions'] = array('Field.label_id' => $id, 'Field.category_id' => $id2);
        if (!empty($id3)) {
            $options['joins'] = array(
                array('table' => 'authentic_product_additional_field_values',
                    'alias' => 'ProductAdditionalFieldValue',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'ProductAdditionalFieldValue.product_id =' . $id3,
                        'ProductAdditionalFieldValue.field_id = Field.id',
                    )
                )
            );
            $options['fields'] = array("Field.id", "Field.category_id", "Field.field_name", "Field.field_type", "Field.field_value", "Field.required", 'ProductAdditionalFieldValue.values', 'ProductAdditionalFieldValue.id');
        }
        return $this->find('all', $options);
    }

    function color_menu() {
        $color = $this->find('all', array('fields' => array('Field.id'), 'conditions' => array('OR' => array(array('Field.field_name LIKE' => '%Color%'), array('Field.field_name LIKE' => '%Colour%'))), 'order' => 'Field.id  asc', 'recursive' => '-1'));
        $results = Set::extract('/Field/id', $color);
        return $this->ProductAdditionalFieldValue->find('all', array('fields' => array('ProductAdditionalFieldValue.values', 'count(ProductAdditionalFieldValue.values) as totalproduct'), 'conditions' => array('ProductAdditionalFieldValue.field_id' => $results), 'order' => 'ProductAdditionalFieldValue.values  asc', 'group' => 'ProductAdditionalFieldValue.values', 'recursive' => '-1'));
    }

}

?>